package com.service.scheduling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.HashRateOrderEnums;
import com.service.dao.enums.MemberJAType;
import com.service.dao.mapper.OrderProductHrComboDetailsMapper;
import com.service.dao.mapper.OrderProductHrComboMapper;
import com.service.dao.mapper.OrderProductHrComboPaddingMapper;
import com.service.dao.model.OrderProductHrCombo;
import com.service.dao.model.OrderProductHrComboDetails;
import com.service.dao.model.OrderProductHrComboPadding;
import com.service.scheduling.dto.MemberStoreChange;
import com.service.scheduling.service.MemberStoreService;
import com.service.scheduling.service.OrderProductHrComboPaddingService;
import com.service.scheduling.support.MemberStoreLockUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("orderProductHrComboPaddingService")
public class OrderProductHrComboPaddingServiceImpl extends ServiceImpl<OrderProductHrComboPaddingMapper, OrderProductHrComboPadding> implements OrderProductHrComboPaddingService {
	@Autowired
	private OrderProductHrComboDetailsMapper comboDetailsMapper;
	@Autowired
	private OrderProductHrComboMapper orderProductHrComboMapper;
	@Autowired
	private MemberStoreLockUtils memberStoreLockUtils;
	@Autowired
	private MemberStoreService memberStoreService;

	/**
	 * @param coinsId
	 * @param startTime
	 * @Description: 获取小于当前时间的填充算力订单
	 * @Author: ty
	 * @Date: 2021/6/8 20:32
	 * @param: [coinsId, startTime]
	 * @return: java.util.List<com.service.dao.model.OrderProductHrComboPadding>
	 */
	@Override
	public List<OrderProductHrComboPadding> listBeforeCurrentDate(Integer coinsId, Date startTime) {
		return baseMapper.listBeforeDate(coinsId, HashRateOrderEnums.getHrComboUsingStatus(), startTime);
	}

	/**
	 * @param paddingIdList
	 * @Description: 修改结束填充算力订单状态
	 * @Author: ty
	 * @Date: 2021/6/8 21:46
	 * @param: [paddingIdList]
	 * @return: void
	 */
	@Override
	public void updatePaddingHrComboRateOrder2Complete(List<OrderProductHrComboPadding> paddingIdList) {
		List<OrderProductHrComboPadding> updateHrComboOrder = paddingIdList.stream().map(item -> {
			//先修改填充算力订单状态  结束
			item.setPaddingStatus(HashRateOrderEnums.Status.complete.getCode());
			return item;
		}).collect(Collectors.toList());

		if (ObjectUtils.isEmpty(updateHrComboOrder)) {
			return;
		}
		updateBatchById(updateHrComboOrder);
		//处理质押退还流程
		paddingIdList.parallelStream().forEach(item->{
			//查询订单
			OrderProductHrCombo returnHrComboPledge = orderProductHrComboMapper.selectById(item.getHrOrderId());
			//判断订单填充算力是否填充完整
			BigDecimal totalPadding = returnHrComboPledge.getTotalPaddingHashRate();
			if(totalPadding.compareTo(returnHrComboPledge.getTotalHashRate()) == 0){
				//判断当前订单下面的填充算力订单是否全部结束
				List<OrderProductHrComboPadding> paddings = baseMapper.selectList(new QueryWrapper<OrderProductHrComboPadding>().lambda()
						.eq(OrderProductHrComboPadding::getHrOrderId, item.getHrOrderId())
						.notIn(OrderProductHrComboPadding::getPaddingStatus, HashRateOrderEnums.Status.complete.getCode())
				);
				if(ObjectUtils.isEmpty(paddings)){
					OrderProductHrComboDetails details = comboDetailsMapper.selectOne(new QueryWrapper<OrderProductHrComboDetails>().lambda()
							.eq(OrderProductHrComboDetails::getHrOrderId, item.getHrOrderId())
					);
					try{
						boolean isLock = memberStoreLockUtils.lock(returnHrComboPledge.getMemberId(), CoinsEnum.FIL.getCode());
						if (!isLock) {
							return;
						}
						if (details.getPledgeNum().compareTo(BigDecimal.ZERO) <= 0) {
							return;
						}
						memberStoreService.changeMemberStore(
								MemberStoreChange.builder()
										.memberId(returnHrComboPledge.getMemberId())
										.coinsId(CoinsEnum.FIL.getCode())
										.coinsName(CoinsEnum.FIL.getName())
										.jaType(MemberJAType.PAY_HR_COMBO_PLEDGE_RETURN)
										.orderId(returnHrComboPledge.getId())
										.orderDetailId(details.getId())
										.changeNum(details.getPledgeNum())
										.remark(MemberJAType.PAY_HR_COMBO_PLEDGE_RETURN.getMsg())
										.build());
					}finally {
						memberStoreLockUtils.unlock(returnHrComboPledge.getMemberId(), CoinsEnum.FIL.getCode());
					}
				}
			}
		});
	}
}
